/*
 * Sketch to demonstrate the BMA180 3-axis Accelerometer and
 * how to interface with it via I2C and the Wiring library.
 *
 */


#include <wiring.h> // for boolean and byte
#include <Wire.h>
#undef round 
#include "BMA180Controller.h"

BMA180Controller bma180;
BMA180Controller::AccelDataSet accel_data;

void setup()
{
  // Initialize the Wiring library to communicate via I2C
  Wire.begin();

  // Initialize the serial interface to log data
  Serial.begin(9600);
  
  // Initialze our BMA180 Controller
  if (BMA180Controller::SUCCESS == bma180.init() )
  {
    Serial.println("BMA180 Initialized");
    Serial.println("time\t\tX\t\tY\t\tZ"); 
  }


}

void loop()
{
  
  // read data from the BMA180Controller. In the future
  // this could be kicked off by an interrupt handler
  if( BMA180Controller::SUCCESS == bma180.readData() )
  {  
    accel_data=bma180.getData();
    
    Serial.print(accel_data.time); Serial.print("\t");
    Serial.print(accel_data.accel_x,6); Serial.print("\t");
    Serial.print(accel_data.accel_y,6); Serial.print("\t");
    Serial.println(accel_data.accel_z,6);
  }
  else
  {
    // delay 10 sec to give the serial monitor time to 
    // dump the data and try to give the user a chance to
    // debug.
    delay(10000);
    if ( ! bma180.isInitialized() )
    {
      if (BMA180Controller::SUCCESS == bma180.init() )
      {
        Serial.println("BMA180 Initialized");   
        Serial.println("time\t\tX\t\tY\t\tZ");  
      }
    }
  }
  
  
  // arbitrary delay
  delay(10);
}
